package codename.carrot.bomberman.activity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import codename.carrot.bomberman.game.gamemessages.PlayerJoinMessage;
import codename.carrot.bomberman.network.NetworkConnection;
import codename.carrot.bomberman.network.NetworkConnection.NetworkState;
import codename.carrot.bomberman.network.client.ClientPostOffice;

/**
 *  Activity that represents the gameLobby as used by connecting clients,
 *  which allows a client to connect to a server etc
 * @author Gruppe A4
 *
 */
public class ClientGameLobbyActivity extends GameLobbyActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
		String ip = prefs.getString("ip", null);
		
		if(ip == null)
			new IpAddressDialog(this);
		else {
			this.onIpAddressTyped(ip);
			Toast.makeText(this, "Connecting with default IP.", Toast.LENGTH_SHORT).show();
		}
	}


	@Override
	public void onStateChange(NetworkState state) {
		super.onStateChange(state);
		if (state == NetworkState.CONNECTED) {
			// Send join-message with the nickname we received from the intent
			// that started
			// the ClientGameLobbyActivity
			PlayerJoinMessage pm = new PlayerJoinMessage(nickname);
			ClientPostOffice.getInstance().sendMessage(pm);			
		}
	}

	/**
	 * Called when an ip address is typed in the dialogbox.
	 * @param ipAddress The ip address to connect to.
	 */
	private void onIpAddressTyped(String ipAddress) {
		if(ipAddress == null || ipAddress.length() < 1 || ipAddress.startsWith(" ")){
			Toast.makeText(this, "No IP-address detected", Toast.LENGTH_SHORT).show();
			this.finish();
		} else {
			NetworkConnection.getInstance().connectAsClient(ipAddress);
			progressBar.setVisibility(View.VISIBLE);
		}
	}


	/**
	 * Shows a dialog that allows a user to input a text as a string representing an IP-address.
	 * @author Group A4
	 *
	 */
	private class IpAddressDialog extends AlertDialog.Builder implements
			DialogInterface.OnClickListener {
		
		/**
		 * The editiable field that holds the address.
		 */
		private EditText ipAddress;

		/**
		 * Constructs the dialog for ip-addresses.
		 * @param arg0 The calling context/activity.
		 */
		public IpAddressDialog(Context arg0) {
			super(arg0);
			this.setTitle("Server IP Address");
			this.ipAddress = new EditText(arg0);
			this.setView(ipAddress);
			this.setPositiveButton("Ok", this);
			this.setNegativeButton("Cancel", this);
			this.show();
		}

		@Override
		public void onClick(DialogInterface dialog, int which) {
			if (which == DialogInterface.BUTTON_POSITIVE)
				onIpAddressTyped(this.ipAddress.getText().toString());
			else
				onIpAddressTyped(null);
			dialog.dismiss();
		}
	}
}
